package com.dd.cloud.user.service.search.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.SearchConstants;
import com.dd.cloud.user.entity.search.SearchHistory;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.user.mapper.search.SearchHistoryMapper;
import com.dd.cloud.user.res.chknowl.ChUserSearchListRes;
import com.dd.cloud.user.service.search.ISearchHistoryService;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户搜索历史表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-07-05
 */
@Service
public class SearchHistoryServiceImpl extends ServiceImpl<SearchHistoryMapper, SearchHistory> implements ISearchHistoryService {

    /**
     * 获取用户的搜索历史
     *
     * @return
     */
    @Override
    public List<SearchHistory> getSearchHistory(String modular) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        QueryWrapper<SearchHistory> qw = new QueryWrapper<>();
        qw.lambda().eq(SearchHistory::getUserId, loginInfo.getId())
                .eq(SearchHistory::getUserType, loginInfo.getType())
                .eq(SearchHistory::getModular, modular)
                .eq(SearchHistory::getStatus, SearchConstants.SearchStatus.NOMER)
                .orderByDesc(SearchHistory::getCreateDate);
        Page<SearchHistory> iPage = new Page(1, 12);
        iPage = getBaseMapper().selectPage(iPage, qw);
        return iPage.getRecords();
    }

    /**
     * 清空搜索历史
     */
    @Override
    public void cleanSearchHistory(String modular) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        UpdateWrapper<SearchHistory> up = new UpdateWrapper<>();
        up.lambda().eq(SearchHistory::getUserId, loginInfo.getId())
                .eq(SearchHistory::getUserType, loginInfo.getType())
                .eq(SearchHistory::getModular, modular)
                .set(SearchHistory::getStatus, SearchConstants.SearchStatus.DEL);
        boolean i = update(up);
        if (!i) {
            throw new ZekeException(ZkExceptionEnum.OPER_ERROR);
        }
    }

    /**
     * 添加搜索历史 如存在则刷新时间
     *
     * @param search
     */
    @Override
    public void addSearchHistory(String search, String modular) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        SearchHistory history;
        QueryWrapper<SearchHistory> uw = new QueryWrapper<SearchHistory>();
        uw.lambda().eq(SearchHistory::getSearch, search)
                .eq(SearchHistory::getUserId, loginInfo.getId())
                .eq(SearchHistory::getStatus, SearchConstants.SearchStatus.NOMER)
                .eq(SearchHistory::getUserType, loginInfo.getType())
                .eq(SearchHistory::getModular, modular);
        List<SearchHistory> list = list(uw);
        if (list != null && !list.isEmpty()) {
            history = list.get(0);
            history.setCreateDate(LocalDateTime.now());
            updateById(history);
            return;
        }
        history = new SearchHistory();
        history.setStatus(SearchConstants.SearchStatus.NOMER);
        history.setModular(modular);
        history.setSearch(search);
        history.setUserId(loginInfo.getId());
        history.setUserType(loginInfo.getType());
        history.setCreateDate(LocalDateTime.now());
        save(history);
    }

    /**
     * 全局搜索中医知识库
     *
     * @param keyword
     * @return
     */
    @Override
    public List<ChUserSearchListRes> queryChByKeyword(String keyword) {
        addSearchHistory(keyword, SearchConstants.Modular.CHSCIENCE);
        return getBaseMapper().searchChByKeyword(keyword);
    }

    @Override
    public void removeHistory(Integer id) {
        SearchHistory searchHistory = this.getById(id);
        if (searchHistory == null) {
            throw new ZekeException("历史记录不存在");
        }
        searchHistory.setStatus(-1);
        Boolean flag = this.updateById(searchHistory);
        if (!flag) {
            throw new ZekeException("删除失败!");
        }
    }
}
